package org.ne.banksys.controller;

import org.ne.banksys.common.Result;
import org.ne.banksys.entity.Card;
import org.ne.banksys.entity.User;
import org.ne.banksys.service.CardService;
import org.ne.banksys.service.DepositService;
import org.ne.banksys.service.UserService;
import org.ne.banksys.utils.CardIdCreateUtils;
import org.ne.banksys.utils.MD5Utils;
import org.ne.banksys.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wey
 * @date 2025/6/16 上午10:18 06
 **/

@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private DepositService depositService;

    @GetMapping("/customerCard")
    public Result getCustomerCard() {
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));
        List<Card> res = cardService.selectCardByCustomerId(id);
        return Result.success(res);
    }

    @PostMapping("/reportLoss")
    public Result reportLoss(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String cardPassword = (String) para.get("cardPass");
        Card card = cardService.selectCardById(cardId);
        if (card == null) {
            return Result.error("card not found");
        }
        if (!card.getPass().equals(MD5Utils.code( cardPassword))) {
            return Result.error("password error");
        }

        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));
        if (!id.equals(card.getCustomerId())) {
            return Result.error("customer id error");
        }
        cardService.reportLoss(cardId);
        return Result.success();
    }

    @PostMapping("/reportReturn")
    public Result reportReturn(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String cardPassword = (String) para.get("cardPass");
        Card card = cardService.selectCardById(cardId);
        if (card == null) {
            return Result.error("card not found");
        }
        if (!card.getPass().equals(MD5Utils.code( cardPassword))) {
            return Result.error("password error");
        }
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));
        if (!id.equals(card.getCustomerId())) {
            return Result.error("customer id error");
        }
        cardService.reportReturn(cardId);
        return Result.success();
    }

    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String oldPwd = (String) para.get("oldPwd");

        String newPwd = (String) para.get("newPwd");
        String rePwd = (String) para.get("rePwd");
        if (oldPwd.equals(newPwd)) {
            return Result.error("new password cannot be the same");
        }
        if (!newPwd.equals(rePwd)) {
            return Result.error("repeat password error");
        }
        String oldMd5 = MD5Utils.code(oldPwd);
        String newMd5 = MD5Utils.code(newPwd);

        Card card = cardService.selectCardById(cardId);
        if (card == null) {
            return Result.error("card not found");
        }
        if (!card.getPass().equals(oldMd5)) {
            return Result.error("old password error");
        }
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));
        if (!id.equals(card.getCustomerId())) {
            return Result.error("customer id error");
        }
        cardService.updatePwd(cardId, newMd5);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String cardPass = (String) para.get("cardPass");
        String savingName = (String) para.get("savingName");
        String curId = (String) para.get("curId");

        Long savingId = depositService.getSavingId(savingName);
        Card card = cardService.selectCardById(cardId);
        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));
        if (card == null) {
            return Result.error("card not found");
        }
        if (!id.equals(card.getCustomerId())) {
            return Result.error("customer id error");
        }

        if (!card.getPass().equals(MD5Utils.code( cardPass))) {
            return Result.error("password error");
        }

        card.setCurId(curId);
        card.setSavingId(savingId);
        cardService.update(card);

        return Result.success(cardService.selectCardById(cardId));

    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody Map<String, Object> para) {
        String cardId = (String) para.get("cardId");
        String cardPassword = (String) para.get("cardPass");

        Card card = cardService.selectCardById(cardId);
        if (card == null) {
            return Result.error("card not found");
        }
        if (!card.getPass().equals(MD5Utils.code( cardPassword))) {
            return Result.error("password error");
        }

        Map<String, Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong((String) info.get("customerId"));

        if (!id.equals(card.getCustomerId())) {
            return Result.error("customer id error");
        }

        cardService.deleteCard(cardId);
        return Result.success();

    }



    // @Insert("insert into card()" +
    //            "values (#{cardId}, 'RMB', #{saving_id}, #{openDate}, #{openMoney}, #{balance}, #{pass}, #{isReportLoss},#{customerId})")
    //    void add(Card card);
    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> para) {
//        String cardId = (String) para.get("cardId");
        String cardPassword = para.get("cardPass").toString();
        BigDecimal amount = BigDecimal.valueOf( Double.parseDouble( para.get("openMoney").toString()) );
        Long savingId = Long.parseLong( para.get("savingId").toString());
//        Long savingId = depositService.getSavingId(savingName);
        String cardId = CardIdCreateUtils.createCardId();

        Card card = new Card();
        Map<String, Object> info = ThreadLocalUtils.get();

        card.setCardId(cardId);
        card.setPass(MD5Utils.code( cardPassword));
        card.setCurId("RMB");
        card.setCustomerId(Long.parseLong((String) info.get("customerId")));
        card.setSavingId(savingId);
        card.setOpenMoney(amount);
        card.setBalance(amount);
        card.setOpenDate(new Timestamp(System.currentTimeMillis()));
        card.setIsReportLoss("否");

        cardService.insert(card);

        return Result.success(cardService.selectCardById(cardId));
    }



}
